package com.wxw.BaseLibrary.element;

import android.graphics.Color;
import android.view.Gravity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by david on 2016/4/8.
 */
public abstract class DefaultDataGridViewDataSource<T extends Object> implements IDataGridViewDataSource {
    protected List<List<T>> items = new ArrayList<List<T>>();
    protected List<DataSourceColumn> columns = new ArrayList<DataSourceColumn>();
    protected int[] bgColors = null;
    protected IDataGridCellViewAgent agent = null;
    protected int iRow = -1;

    public DefaultDataGridViewDataSource(){
        init();
    }

    public DefaultDataGridViewDataSource(IDataGridCellViewAgent agent) {
        this.agent = agent;
        init();
    }

    @Override
    public int size() {
        return items.size();
    }

    @Override
    public DataSourceColumn getColumn(int i) {
        return (columns == null || columns.size() <= 0 || i < 0 || i >= columns.size()) ? null : columns.get(i);
    }

    @Override
    public int getColumnSize() {
        return columns.size();
    }

    @Override
    public String getColumnName(int i) {
        if (columns == null || columns.size() <= 0 || i < 0 || i >= columns.size()) {
            return "";
        }
        return columns.get(i).Name;
    }

    @Override
    public int getColumnTextGravity(int i) {
        if (columns == null || columns.size() <= 0 || i < 0 || i >= columns.size()) {
            return Gravity.NO_GRAVITY;
        }
        return columns.get(i).TextGravity;
    }

    @Override
    public int getColumnWidth(int i) {
        if (columns == null || columns.size() <= 0 || i < 0 || i >= columns.size()) {
            return 0;
        }
        return columns.get(i).Width;
    }

    @Override
    public String getColumnFloatFormat(int i) {
        if (columns == null || columns.size() <= 0 || i < 0 || i >= columns.size()) {
            return "";
        }
        return columns.get(i).FloatFormat;
    }

    @Override
    public boolean moveToPosition(int i) {
        if (items == null || items.size() <= 0 || i < 0 || i >= items.size()) {
            return false;
        }
        iRow = i;
        return true;
    }

    @Override
    public String getValue(int row, int col) {
        return getObject(row, col).toString();
    }

    @Override
    public String getValue(int col) {
        return getValue(iRow, col);
    }

    @Override
    public Object getObject(int row, int col) {
        if (checkRowAndCol(row, col)) {
            if (items.size()>row) {
                List<T> item = items.get(row);
                if (item.size()>col) {
                    return item.get(col);
                }
            }
        }
        return null;
    }

    @Override
    public void setValue(int row, int col, String value) {

    }

    @Override
    public void newDataRow() {
        items.add(new ArrayList<T>());
    }

    @Override
    public int getRowBgColor(int row) {
        return Color.TRANSPARENT;
    }

    protected abstract void init();

    private boolean checkRowAndCol(int r, int c) {
        if (items == null || items.size() <= 0 || r < 0 || r >= items.size()) {
            return false;
        }
        if (columns == null || columns.size() <= 0 || c < 0 || c >= columns.size()) {
            return false;
        }
        return true;
    }
}
